<?php

// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2030 http://www.jmzh1688.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: lxl
// | jmzhfsauto
// +----------------------------------------------------------------------


namespace app\admin\controller;

use think\Controller;
use think\Db;

use app\admin\model\FriendarticleModel;


class Friendarticle extends Base
{



    public function getWhereFromPara()
    {
        $param = input('param.');
        $where=[];


        

        
        return $where;
    }


    
    public function _initialize()
    {
        $wherep=$this->getWhereFromPara();
        $furl=url('friendarticle/index' ,$wherep);
        $faddurl=url('friendarticle/add' ,$wherep);
        $fediturl=url('friendarticle/edit' ,$wherep);
        
        $this->assign([
            'nowurl' => $furl,
            'addurl' => $faddurl,
            'editurl'=> $fediturl
            
        ]);
       
    }
 

    public function index()
    {

        $wherep=$this->getWhereFromPara();
    

        if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $friendarticlemodel = new FriendarticleModel();


            $where = getWhereForm( $param);
            $where['isdelete']=0;
            $whereor=[];

            

            $where=array_merge($where,$wherep);
    

            $list = $friendarticlemodel->getByWhere($where, $offset, $limit, $whereor);
            foreach($list as $key=>$vo){
                $list[$key]['operate'] = $this->getRowButton($vo['id'],$wherep);
            }
            $result['total'] =$friendarticlemodel->getAllCount($where, $whereor);  // 总数据
            $result['rows'] = $list;
            return json($result);
        }
 

        return $this->fetch();

    }

    public function del()
    {
        $friendarticlemodel = new FriendarticleModel();
        if(request()->isPost()){
            $id = input("post.id");
            $msg = $friendarticlemodel->delById($id);
            return json(msg($msg['code'], $msg['data'], $msg['msg']));
        }
        return $this->fetch();
    }

    public function edit()
    {
        $friendarticlemodel = new FriendarticleModel();

        if(request()->isPost()){
            $param = input("post.");

 
            foreach ($param as $key=>$value){
                if(is_array($value))
                {
                    $param[$key]=implode(",",$param[$key]);
                }
            }
           
            $wherep=$this->getWhereFromPara();
            $parastr="";
            foreach ($wherep as $key => $value)
            {
                $parastr=$parastr.$key."=".$value."&";
            }

            $msg = $friendarticlemodel->edit($param);
            return json(msg($msg['code'], $msg['data']."?". $parastr, $msg['msg']));
        }
      
        $id = input('param.id');
        $info=$friendarticlemodel->getById($id);
  
        $this->assign([
            'info' => $info
        ]);
        return $this->fetch();
    }

    public function add()
    {
    

        if(request()->isPost()){
            $friendarticlemodel = new FriendarticleModel();
            $param = input("post.");
            $wherep=$this->getWhereFromPara();
            $param=array_merge($param,$wherep);

           
            $parastr="";
            foreach ($wherep as $key => $value)
            {
                $parastr=$parastr.$key."=".$value."&";
            }


            

            $msg = $friendarticlemodel->add($param);
            return json(msg($msg['code'], $msg['data']."?". $parastr, $msg['msg']));
        }
    
        return $this->fetch();
    }



  
        private function getRowButton($id,$wherep)
        {
           
            $operatestr= '[{"uuid":"edit","title":"编辑","href":"'.url('edit', ['id' => $id]).'","icon":"","style":"","para":"","isnouse":0},
{"uuid":"del","title":"删除","href":" javascript:del(this,'.$id.')","icon":"","style":"","para":"","isnouse":0}]';
    
               $operate=json_decode($operatestr,true);
              $option= getButtonHtml($operate);
                return $option;
    
        }
    
}
